package myTest;

import java.util.Base64;

public class BASE64Test {
    final static Base64.Decoder decoder = Base64.getDecoder();
    final static Base64.Encoder encoder = Base64.getEncoder();

    public static void main(String[] args) {
        String s = "";
        System.out.println("原字符串：" + s);
        String encryptString = encryptBASE64(s);
        System.out.println("加密后：" + encryptString);
        System.out.println("解密后：" + decryptBASE64(encryptString));
        System.out.println(decryptBASE64("5paw55aG5Ye6546w5LqG5Liq6ZuG5Lit6JCl77yM5YWz5oq85LqGMTDlpJpX57u05ZC+5bCU5peP77yM5bey57uP5pCe5Yiw6IGU5ZCI5Zu95LqG77yM576O5Zu96KaB5Yi26KOB5LqG"));
    }

    /**
     * BASE64解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String decryptBASE64(String key) {
        byte[] bt;
        bt = decoder.decode(key);
        return new String(bt);//如果出现乱码可以改成： String(bt, "utf-8")或 gbk

    }

    /**
     * BASE64加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(String key) {
        byte[] bt = key.getBytes();
        return encoder.encodeToString(bt);
    }
}
